package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.base.utils.UploadFile;
import com.qy.model.Banner;
import com.qy.service.BannerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zaq on 2018/06/21.
 */
@RestController("Manage-BannerController")
@RequestMapping("/manage/banner")
public class BannerController {
    @Resource
    private BannerService bannerService;

    @PostMapping("/add")
    public Result add(@RequestBody Banner banner) {
        bannerService.save(banner);
        return ResultGenerator.successResult();
    }

    //删除
    @PostMapping("/delete")
    public Result delete(@RequestBody Banner banner) {
        if (banner.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        bannerService.deleteById(banner.getId());
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Banner banner) {
        bannerService.update(banner);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Banner banner = bannerService.findById(id);
        return ResultGenerator.successResult(banner);
    }

    //轮播图列表
    @GetMapping("/list")
    public Result list(PageBean<Banner> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize() , "add_time desc");
        List<Banner> list = bannerService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //添加轮播图
    @PostMapping("/addBanner")
    public Result addBanner(@RequestBody Banner banner) {
        if (banner.getPicture() == null || banner.getPicture().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        } else {
            banner.setPicture(UploadFile.uploadBase64(banner.getPicture()));
        }

        if (banner.getSort() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        banner.setAdd_time(DateUtil.getNowTimestamp());
        bannerService.save(banner);
        return ResultGenerator.successResult();
    }

    //上架
    @PostMapping("/shelf")
    public Result shelf(@RequestBody Banner banner) {
        if (banner.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Banner banner1 = bannerService.findById(banner.getId());
        banner1.setState(1);
        bannerService.update(banner1);
        return ResultGenerator.successResult();
    }

    //下架
    @PostMapping("/lower")
    public Result lower(@RequestBody Banner banner) {
        if (banner.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Banner banner1 = bannerService.findById(banner.getId());
        banner1.setState(2);
        bannerService.update(banner1);
        return ResultGenerator.successResult();
    }

}
